﻿using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Verse;
using Verse.AI.Group;

namespace SZ.Mech
{
    public class CompProperties_AddCommandRanage : CompProperties
    {
        public CompProperties_AddCommandRanage()
        {
            this.compClass = typeof(CommandRanage);
        }
    }
    public class CommandRanage : ThingComp
    {
        Pawn pawn
        {
            get
            {
                return this.parent as Pawn;
            }
        }

        Pawn masterPawn;
        //机械体在机械者离开地图后仍能自动完成工作，不会离线
        public override void Initialize(CompProperties props)
        {
            base.Initialize(props);

            if (this.pawn.IsColonyMechPlayerControlled)
            {
                foreach (var pawn in this.pawn.Map.mapPawns.FreeColonists)
                {
                    if (pawn.mechanitor.CanControlMechs)
                    {
                        if (!pawn.health.hediffSet.HasHediff(SZ_DefOf.Command_Range))
                        {
                            HediffCommandRange commandRange = (HediffCommandRange)pawn.health.AddHediff(SZ_DefOf.Command_Range);
                            commandRange.owner = this.pawn;
                        }
                    }
                }
            }
        }


        //飞天后他的AI被调整过了
        public override void CompTickRare()
        {
            base.CompTickRare();


            var energy = pawn.needs.TryGetNeed<Need_MechEnergy>();
            energy.CurLevel = 100f;
            //var ct = pawn.thinker.ConstantThinkTree;
            //var lord = pawn.GetLord();//不知道为什么会塞Lord，移除就好
            //lord.RemovePawn(pawn);
            //var g = pawn.CurJobDef;
            //var tree = pawn.thinker.MainThinkTree;
            //var constantThinkNodeRoot = pawn.thinker.ConstantThinkNodeRoot;
            //var mainThinkNodeRoot = pawn.thinker.MainThinkNodeRoot;

            if (this.pawn.IsColonyMechPlayerControlled)
            {
                foreach(var pawn in this.pawn.Map.mapPawns.FreeColonists)
                {
                    if (pawn.mechanitor != null)
                    {
                        if (pawn.mechanitor.CanControlMechs)
                        {
                            if (!pawn.health.hediffSet.HasHediff(SZ_DefOf.Command_Range))
                            {
                                HediffCommandRange commandRange = (HediffCommandRange)pawn.health.AddHediff(SZ_DefOf.Command_Range);
                                commandRange.owner = this.pawn;
                            }
                        }
                    }
                }
            }
        }

        public override void PostDeSpawn(Map map)
        {
            base.PostDeSpawn(map);
        }
        

    }

}
